package com.lgx.right.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lgx.common.entity.Dictionary;
import com.lgx.common.exception.DataException;
import com.lgx.common.web.Constants;
import com.lgx.common.web.Pagination;
import com.lgx.common.web.json.Json2Utils;
import com.lgx.common.web.util.DictUtils;
import com.lgx.common.web.util.EscapeUtils;
import com.lgx.right.entity.Organization;
import com.lgx.right.entity.User;
import com.lgx.right.service.IOrganizationService;
import com.lgx.right.service.IUserService;

@Controller
@RequestMapping("/organization")
public class OrganizationController {

	@Autowired
	private IOrganizationService organizationService;
	@Autowired
	private IUserService userService;

	public IOrganizationService getOrganizationService() {
		return organizationService;
	}

	public void setUserService(IUserService userService) {
		this.userService = userService;
	}

	public void setOrganizationService(IOrganizationService organizationService) {
		this.organizationService = organizationService;
	}
	
	@RequestMapping("/organizationMgr.do")
	public String organizationMgr(HttpServletRequest request,HttpServletResponse response)
	{
		String otype = request.getParameter("otype");
		request.setAttribute("otype", otype);
		return "right/organization/organizationMgr";
	}
	
	@RequestMapping("/list.do")
	public void list(HttpServletRequest request,HttpServletResponse response)
	{
		String pageNum = request.getParameter("page");
		String pageSize= request.getParameter("rows");
		
		if(StringUtils.isEmpty(pageSize))pageSize = Constants.PAGE_SIZE;
		if(StringUtils.isEmpty(pageNum))pageNum = Constants.FIRST_PAGE_NUM;
		

		Object[] params = new Object[10];
		String type = request.getParameter("type");
		params[0] = type;
		String name = request.getParameter("name");
		if(!StringUtils.isEmpty(name))
		{
			params[1] = name;
		}
		String code = request.getParameter("code");
		if(!StringUtils.isEmpty(code))
		{
			params[2] = code;
		}
		String tel = request.getParameter("tel");
		if(!StringUtils.isEmpty(tel))
		{
			params[3] = tel;
		}
		String parent = request.getParameter("parent");
		if(!StringUtils.isEmpty(parent))
		{
			params[4] = parent;
		}
		
		
		Pagination page = this.organizationService.findOrganizationPagination(params, Integer.parseInt(pageSize), Integer.parseInt(pageNum));
		
		Json2Utils.parseJSON(page, response);
	}
	
	@RequestMapping("/init.do")
	public String init(HttpServletRequest request,HttpServletResponse response)
	{
		String type = request.getParameter("type");
		String otype = request.getParameter("otype");
		String organizationId = request.getParameter("organizationId");
		if("edit".equals(type)){
			Organization organization = this.organizationService.findOrganizationById(Long.parseLong(organizationId));
			request.setAttribute("organization", organization);
		}
		List<Dictionary> issend = DictUtils.getDictListByKind(DictUtils.IS_SEND);
		request.setAttribute("issend", issend);
		List<Dictionary> star = DictUtils.getDictListByKind(DictUtils.MERCHANT_STAR);
		request.setAttribute("star", star);

		
		request.setAttribute("type", type);
		request.setAttribute("otype", otype);
		request.setAttribute("organizationId", organizationId);
		
		if("1".equals(otype)){
			return "right/organization/organization-init";
		}else {
			return "right/organization/organization-add";
		}
		
		
	}
	
	@RequestMapping("/listParent.do")
	public void listParent(HttpServletRequest request,HttpServletResponse response)
	{
		String type = request.getParameter("type");
		List<Organization> list = this.organizationService.findOrganizationParent(type);	
		request.setAttribute("type", type);
		Json2Utils.parseJSON(list, response);
	}
	
	@RequestMapping("/save.do")
	@ResponseBody
	public String save(HttpServletRequest request,HttpServletResponse response) {
		String otype = request.getParameter("otype");
		
		String name = request.getParameter("name");
		if(!StringUtils.isEmpty(name))name = EscapeUtils.unescape(name);	
		String code = request.getParameter("code");
		if(!StringUtils.isEmpty(code))code = EscapeUtils.unescape(code);
		String legalId = request.getParameter("legal");
		if(!StringUtils.isEmpty(legalId))legalId = EscapeUtils.unescape(legalId);
		String province = request.getParameter("province");
		if(!StringUtils.isEmpty(province))province = EscapeUtils.unescape(province);
		String city = request.getParameter("city");
		if(!StringUtils.isEmpty(city))city = EscapeUtils.unescape(city);	
		String country = request.getParameter("county");
		if(!StringUtils.isEmpty(country))country = EscapeUtils.unescape(country);
		String address = request.getParameter("address");
		if(!StringUtils.isEmpty(address))address = EscapeUtils.unescape(address);
		String pointsX = request.getParameter("pointsX");
		if(!StringUtils.isEmpty(pointsX))pointsX = EscapeUtils.unescape(pointsX);
		String pointsY = request.getParameter("pointsY");
		if(!StringUtils.isEmpty(pointsY))pointsY = EscapeUtils.unescape(pointsY);	
		String tel = request.getParameter("tel");
		if(!StringUtils.isEmpty(tel))tel = EscapeUtils.unescape(tel);	
		String isSend = request.getParameter("isSend");
		if(!StringUtils.isEmpty(isSend))isSend = EscapeUtils.unescape(isSend);
		String star = request.getParameter("star");
		if(!StringUtils.isEmpty(star))star = EscapeUtils.unescape(star);
		String parentId = request.getParameter("parentId");
		if(!StringUtils.isEmpty(parentId))parentId = EscapeUtils.unescape(parentId);
		String logoUrl = request.getParameter("logoUrl");
		if(!StringUtils.isEmpty(logoUrl))logoUrl = EscapeUtils.unescape(logoUrl);	
		String businessTime = request.getParameter("businessTime");
		if(!StringUtils.isEmpty(businessTime))businessTime = EscapeUtils.unescape(businessTime);
		String reserveTime = request.getParameter("reserveTime");
		if(!StringUtils.isEmpty(reserveTime))reserveTime = EscapeUtils.unescape(reserveTime);
		String reserveTel = request.getParameter("reserveTel");
		if(!StringUtils.isEmpty(reserveTel))reserveTel = EscapeUtils.unescape(reserveTel);
		String busLine = request.getParameter("busLine");
		if(!StringUtils.isEmpty(busLine))busLine = EscapeUtils.unescape(busLine);
		String description = request.getParameter("description");
		if(!StringUtils.isEmpty(description))description = EscapeUtils.unescape(description);
		String areaNode = request.getParameter("areaNode");
		if(!StringUtils.isEmpty(areaNode))areaNode = EscapeUtils.unescape(areaNode);
		String areaName = request.getParameter("areaName");
		if(!StringUtils.isEmpty(areaNode))areaName = EscapeUtils.unescape(areaName);
		
		

		Organization organization = new Organization();
		organization.setName(name);
		organization.setCode(code);
		organization.setProvince(province);
		organization.setCity(city);
		organization.setCounty(country);
		organization.setAddress(address);
		if(!StringUtils.isEmpty(pointsX) && StringUtils.isEmpty(pointsY)) {
			organization.setPoints(pointsX+","+pointsY);
		}	
		organization.setTel(tel);
		organization.setIsSend(isSend);
		organization.setStar(star);
		organization.setLogoUrl(logoUrl);
		organization.setBusinessTime(businessTime);
		organization.setReserveTime(reserveTime);
		organization.setReserveTel(reserveTel);
		organization.setBusLine(busLine);
		organization.setDescription(description);
		organization.setType(otype);
		organization.setCreateDate(new Date());
		organization.setAreaNode(areaNode);
		organization.setAreaName(areaName);
		
		User onlineUser = (User) request.getSession().getAttribute(Constants.LOGIN_USER);
		
		organization.setCreaterId(onlineUser.getId()+"");
		organization.setIsDelete("0");
		
		
		if(!StringUtils.isEmpty(parentId)){
			Organization parent = this.organizationService.findOrganizationById(Long.parseLong(parentId));
			organization.setParent(parent);
		}
		
		if(!StringUtils.isEmpty(legalId)){
			User user = this.userService.findUserById(Long.parseLong(legalId));
			organization.setLegal(user);
		}
		
		try {
			this.organizationService.createOrganization(organization);
			
			User legal = organization.getLegal();
			legal.setOrgId(organization.getId());
			legal.setOrgName(organization.getName());
			legal.setOrgType(organization.getType());
			legal.setIsMaster("0");
			
			this.userService.updateUser(legal);
			
			return "1";
		} catch (DataException e) {
			e.printStackTrace();
			return "0";
		}
		
	}
	
	
	@RequestMapping("/update.do")
	@ResponseBody
	public String update(HttpServletRequest request,HttpServletResponse response) {
		String otype = request.getParameter("otype");
		
		String organizationId = request.getParameter("organizationId");
		Organization organization = this.organizationService.findOrganizationById(Long.parseLong(organizationId));
		
		String name = request.getParameter("name");
		if(!StringUtils.isEmpty(name))name = EscapeUtils.unescape(name);	
		String code = request.getParameter("code");
		if(!StringUtils.isEmpty(code))code = EscapeUtils.unescape(code);
		String legalId = request.getParameter("legal");
		if(!StringUtils.isEmpty(legalId))legalId = EscapeUtils.unescape(legalId);
		String province = request.getParameter("province");
		if(!StringUtils.isEmpty(province))province = EscapeUtils.unescape(province);
		String city = request.getParameter("city");
		if(!StringUtils.isEmpty(city))city = EscapeUtils.unescape(city);	
		String county = request.getParameter("county");
		if(!StringUtils.isEmpty(county))county = EscapeUtils.unescape(county);
		String address = request.getParameter("address");
		if(!StringUtils.isEmpty(address))address = EscapeUtils.unescape(address);
		String pointsX = request.getParameter("pointsX");
		if(!StringUtils.isEmpty(pointsX))pointsX = EscapeUtils.unescape(pointsX);
		String pointsY = request.getParameter("pointsY");
		if(!StringUtils.isEmpty(pointsY))pointsY = EscapeUtils.unescape(pointsY);	
		String tel = request.getParameter("tel");
		if(!StringUtils.isEmpty(tel))tel = EscapeUtils.unescape(tel);	
		String isSend = request.getParameter("isSend");
		if(!StringUtils.isEmpty(isSend))isSend = EscapeUtils.unescape(isSend);
		String star = request.getParameter("star");
		if(!StringUtils.isEmpty(star))star = EscapeUtils.unescape(star);
		String logoUrl = request.getParameter("logoUrl");
		if(!StringUtils.isEmpty(logoUrl))logoUrl = EscapeUtils.unescape(logoUrl);	
		String businessTime = request.getParameter("businessTime");
		if(!StringUtils.isEmpty(businessTime))businessTime = EscapeUtils.unescape(businessTime);
		String reserveTime = request.getParameter("reserveTime");
		if(!StringUtils.isEmpty(reserveTime))reserveTime = EscapeUtils.unescape(reserveTime);
		String reserveTel = request.getParameter("reserveTel");
		if(!StringUtils.isEmpty(reserveTel))reserveTel = EscapeUtils.unescape(reserveTel);
		String busLine = request.getParameter("busLine");
		if(!StringUtils.isEmpty(busLine))busLine = EscapeUtils.unescape(busLine);
		String description = request.getParameter("description");
		if(!StringUtils.isEmpty(description))description = EscapeUtils.unescape(description);
		String areaNode = request.getParameter("areaNode");
		if(!StringUtils.isEmpty(areaNode))areaNode = EscapeUtils.unescape(areaNode);
		String areaName = request.getParameter("areaName");
		if(!StringUtils.isEmpty(areaNode))areaName = EscapeUtils.unescape(areaName);
		String parentId = request.getParameter("parentId");
		if(!StringUtils.isEmpty(parentId))parentId = EscapeUtils.unescape(parentId);
		

		organization.setName(name);
		organization.setCode(code);
		organization.setProvince(province);
		organization.setCity(city);
		organization.setCounty(county);
		organization.setAddress(address);
		if(!StringUtils.isEmpty(pointsX) && StringUtils.isEmpty(pointsY)) {
			organization.setPoints(pointsX+","+pointsY);
		}
		organization.setTel(tel);
		organization.setIsSend(isSend);
		organization.setStar(star);
		organization.setLogoUrl(logoUrl);
		organization.setBusinessTime(businessTime);
		organization.setReserveTime(reserveTime);
		organization.setReserveTel(reserveTel);
		organization.setBusLine(busLine);
		organization.setDescription(description);
		organization.setAreaNode(areaNode);
		organization.setAreaName(areaName);
		organization.setType(otype);
		
		if(!StringUtils.isEmpty(parentId)){
			Organization parent = this.organizationService.findOrganizationById(Long.parseLong(parentId));
			organization.setParent(parent);
		}
		
		if(!StringUtils.isEmpty(legalId)){
			User user = this.userService.findUserById(Long.parseLong(legalId));
			organization.setLegal(user);
		}
		
		try {
			this.organizationService.updateOrganization(organization);
			
			User legal = organization.getLegal();
			legal.setOrgId(organization.getId());
			legal.setOrgName(organization.getName());
			legal.setOrgType(organization.getType());
			legal.setIsMaster("0");
			
			this.userService.updateUser(legal);
			
			return "1";
		} catch (DataException e) {
			e.printStackTrace();
			return "0";
		}
		
	}
	
	
	@RequestMapping("/delete.do")
	@ResponseBody
	public String delete(HttpServletRequest request,HttpServletResponse response)
	{
		String ids = request.getParameter("ids");
		try {
			this.organizationService.deleteOrganizations(ids.split(","));
			return "1";
		} catch (Exception e) {
			e.printStackTrace();
			return "0";
		}
	}
	
	
	/**
	 * 修改当前登录用户的相关商家信息
	 * 此方法与本模块无关联
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/loginUserPer.do")
	public String loginUserPer(HttpServletRequest request,HttpServletResponse response)
	{
		String otype = request.getParameter("otype");    //orgtype
		String organizationId = request.getParameter("organizationId");
		//依照id查找到对象，返回
		Organization organization = this.organizationService.findOrganizationById(Long.parseLong(organizationId));
		request.setAttribute("organization", organization);
		
		//数据字典,短信状态标识
		List<Dictionary> issend = DictUtils.getDictListByKind(DictUtils.IS_SEND);
		request.setAttribute("issend", issend);
		
		//数据字典，商家星级
		List<Dictionary> star = DictUtils.getDictListByKind(DictUtils.MERCHANT_STAR);
		request.setAttribute("star", star);

		
		request.setAttribute("otype", otype);
		request.setAttribute("organizationId", organizationId);
		String url = "";
		if(otype.equals("1")){   //表示类型为商家
			url = "common/user/organizationInfo";
		}else{
			url = "common/user/businessInfo";
		}
		
		return url;
	}
}
